#include<iostream>
#include<cstring>
using namespace std;
typedef pair<int, int> PII;
const int N = 155;
#define x first
#define y second
char map[N][N];
int n, m;
PII q[N * N];
int dis[N][N];
int bfs()
{
	int sx = 0, sy = 0;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			if (map[i][j] == 'K') {
				sx = i;
				sy = j;
			}
		}
	}
	int dx[] = { -2,-1,1,2,2,1,-1,-2 };
	int dy[] = { 1,2,2,1,-1,-2,-2,-1 };
	int hh = 0;
	int tt = 0;
	q[0] = { sx,sy };
	memset(dis, -1, sizeof(dis));
	dis[sx][sy] = 0;
	while (hh <= tt) {
		PII t = q[hh++];
		for (int i = 0; i < 8; i++) {
			int a = t.x + dx[i];
			int b = t.y + dy[i];
			if (a < 0 || a >= n || b < 0 || b >= m) continue;
			if (dis[a][b] != -1)continue;
			if (map[a][b] == '*')continue;
			if (map[a][b] == 'H') {
				dis[a][b] = dis[t.x][t.y] + 1;
				return dis[a][b];
			}
			dis[a][b] = dis[t.x][t.y] + 1;
			q[++tt] = { a,b };
		}
	}
	return -1;
}
int main()
{
	cin >> m >> n;;
	for (int i = 0; i < n; i++)cin >> map[i];
	int ret = bfs();
	cout << ret << endl;
	return 0;
}